<template>
	<view>
		<!-- 搜索 -->
		<view class="search-box">
			<my-search @myClick="gotoSearch"></my-search>
		</view>
		<!-- 滑动区域 -->
		<view class="container">
			<!-- 左侧的滑动区域 -->
			<scroll-view class="left" scroll-y :style="{ height: wh + 'px' }">
				<view class="item" :data-id="index" :class="index === active && 'active'" v-for="(item, index) in cateList" :key="index" @click="activeChange(index)">
					{{ item.cat_name }}
				</view>
			</scroll-view>
			<!-- 右侧的滑动区域 -->
			<scroll-view class="right" scroll-y :style="{ height: wh + 'px' }" :scroll-top="scrollTop">
				<view class="item2" v-for="(item2, index2) in cateLevel2" :key="index2">
					<!-- 二级分类标题 -->
					<view class="title">/{{ item2.cat_name }}/</view>
					<!-- 三级分类盒子 -->
					<view class="item3_list">
						<!-- 三级分类每一项 -->
						<view class="item3" v-for="(item3, index3) in item2.children" :key="index3" @click="gotoGoodsList(item3)">
							<image :src="item3.cat_icon"></image>
							<!-- 三级分类的文本 -->
							<view class="title">{{ item3.cat_name }}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			wh: 0,
			cateList: [],
			cateLevel2: [],
			active: 0,
			scrollTop: 0
		}
	},
	onLoad() {
		// 获取屏幕高度
		const sysInfo = uni.getSystemInfoAsync()
		sysInfo.then(({ windowHeight }) => {
			this.wh = windowHeight
		})
		// 获取分类列表
		this.getCateList()
	},
	methods: {
		// 获取分类列表方法
		async getCateList() {
			const { data: res } = await uni.$http.get('/api/public/v1/categories')
			if (res.meta.status !== 200) return uni.$showMsg()
			this.cateList = res.message
			this.cateLevel2 = res.message[0].children
			// 换地址
			this.cateLevel2.forEach((l2) => {
				l2.children.forEach((l3) => {
					l3.cat_icon = l3.cat_icon.replace('https', 'http')
				})
			})
		},
		// 切换active项
		activeChange(index) {
			this.active = index
			this.cateLevel2 = this.cateList[index].children
			// 换地址
			this.cateLevel2.forEach((l2) => {
				l2.children.forEach((l3) => {
					l3.cat_icon = l3.cat_icon.replace('https', 'http')
				})
			})
			this.scrollTop = this.scrollTop ? 0 : 1
		},
		// 跳转到商品列表页面
		gotoGoodsList(item) {
			uni.navigateTo({
				url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
			})
		},
		gotoSearch() {
			console.log('去搜索')
			uni.navigateTo({
				url: '/subpkg/search/search'
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.search-box {
	z-index: 100;
	position: fixed;
	top: 0;
	width: 100%;
}
.container {
	display: flex;
	.item {
		height: 60px;
		text-align: center;
		line-height: 60px;
	}
	.left {
		width: 120px;
		height: 300px;
		font-size: 12px;
		background-color: #f7f7f7;

		.active {
			position: relative;
			background-color: white;
			&::before {
				content: '';
				display: block;
				position: absolute;
				width: 3px;
				height: 40px;
				top: 50%;
				left: 0;
				transform: translateY(-50%);
				background-color: red;
			}
		}
	}
	.right {
		.item2 {
			font-size: 12px;
			text-align: center;
			padding: 15px 0;
			.title {
				font-weight: bold;
				margin-bottom: 20px;
			}
			.item3_list {
				display: flex;
				flex-wrap: wrap;
				.item3 {
					width: 33.33%;
					image {
						width: 60px;
						height: 60px;
					}
					.title {
						font-size: 12px;
					}
				}
			}
		}
	}
}
</style>
